home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / graphics / 3dvect37.zip / JOYSTICK.ASM < prev    next >
Assembly Source File  |  1994-06-22  |  13KB  |  445 lines

  1. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2. ;
  3. ; Filename     : joystick.asm
  4. ; Included from: Main Assembley Module
  5. ; Description  : Joystick routines
  6. ;
  7. ; Written by: John McCarthy
  8. ;             1316 Redwood Lane
  9. ;             Pickering, Ontario.
  10. ;             Canada, Earth, Milky Way (for those out-of-towners)
  11. ;             L1X 1C5
  12. ;
  13. ; Internet/Usenet:  BRIAN.MCCARTHY@CANREM.COM
  14. ;         Fidonet:  Brian McCarthy 1:229/15
  15. ;   RIME/Relaynet: ->CRS
  16. ;
  17. ; Home phone, (905) 831-1944, don't call at 2 am eh!
  18. ;
  19. ; Send me your protected mode source code!
  20. ; Send me your Objects!
  21. ; But most of all, Send me a postcard!!!!
  22. ;
  23. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  24.  
  25.          .386p
  26.          jumps
  27.  
  28. code32   segment para public use32
  29.          assume cs:code32, ds:code32
  30.  
  31.          include pmode.ext       ; protected mode externals by TRAN
  32.          include xmouse.ext      ; xmode mouse externals
  33.          include xmode.ext       ; xmode externals by matt pritchard
  34.          include irq.ext
  35.          include 3d.ext
  36.          include font.ext
  37.          include file.ext
  38.  
  39.          include macros.inc
  40.          include equ.inc
  41.  
  42.          public _tax
  43.          public _tay
  44.          public _tbx
  45.          public _tby
  46.          public _g_lastcallstate
  47.          public _g_thiscallstate
  48.  
  49.          public _joycenax
  50.          public _joycenay
  51.          public _joycenbx
  52.          public _joycenby
  53.          public _joy_ax
  54.          public _joy_ay
  55.          public _joy_bx
  56.          public _joy_by
  57.  
  58.          public _dead_zone
  59.  
  60.          public _rawjoystick
  61.          public _justgetbutton
  62.          public _calibrate_joystick
  63.          public _wait_all_off
  64.          public _cartisian_joystick
  65.  
  66. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  67. ;port 201h :
  68. ;
  69. ;  bit    JOYSTICK       PADDLE
  70. ;  ---    --------       ------
  71. ;  7      B  button 2    D button
  72. ;  6      B  button 1    C button
  73. ;  5      A  button 2    B button
  74. ;  4      A  button 1    A button
  75. ;  3      B  y-axis      D Coordinate
  76. ;  2      B  x-axis      C Coordinate
  77. ;  1      A  y-axis      B Coordinate
  78. ;  0      A  x-axis      A Coordinate
  79. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  80.  
  81.        time_out equ 3000  ; time out before abort
  82.        joyport  equ 0201h ; joystick port address
  83.  
  84. _tax             dd 0     ; joystick raw data
  85. _tay             dd 0
  86. _tbx             dd 0
  87. _tby             dd 0
  88. _g_lastcallstate db 0     ; last button state / button down      = 0f0h
  89. _g_thiscallstate db 0     ; this button state / button up states = 00fh
  90.  
  91. _joycenax    dd 0         ; center of joystick upon entry
  92. _joycenay    dd 0
  93. _joycenbx    dd 0
  94. _joycenby    dd 0
  95. _joy_ax      dd 0         ; cartisian joystick co-ordinates
  96. _joy_ay      dd 0
  97. _joy_bx      dd 0
  98. _joy_by      dd 0
  99.  
  100. _dead_zone   dd 1000      ; dead zone where centered joystick has no effect
  101.  
  102. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  103. ; Rawjoystick: Get raw joystick data
  104. ; In: null
  105. ; Out:
  106. ;   EAX - x of joystick a
  107. ;   EBX - y of joystick a
  108. ;   ECX - x of joystick b
  109. ;   EDX - y of joystick b
  110. ; Notes:
  111. ; Machine dependant!
  112. ; Routine sets timers running and flags results as they come up.
  113. ; This way, all timers are checked at once.
  114. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  115.  
  116. _rawjoystick:
  117.          xor bl,bl       ; reset attempt flag, when all ports come in, exit
  118.  
  119.          cmp _tax,-1     ; test if port operational
  120.          je raw_misxa
  121.          inc bl          ; count number of ports waiting to come in
  122.          mov _tax,0      ; init values, -1 = port inactive
  123. raw_misxa:
  124.          cmp _tay,-1
  125.          je raw_misya
  126.          inc bl
  127.          mov _tay,0
  128. raw_misya:
  129.          cmp _tbx,-1
  130.          je raw_misxb
  131.          inc bl
  132.          mov _tbx,0
  133. raw_misxb:
  134.          cmp _tby,-1
  135.          je raw_misyb
  136.          inc bl
  137.          mov _tby,0
  138. raw_misyb:
  139.  
  140.          cli             ; irq's off
  141.          mov dx,joyport  ; joyport address
  142.          out dx,al
  143.  
  144.          mov ecx,0       ; reset timer
  145.          align 16
  146.  
  147. joyloop:
  148.          in al,dx        ; get bits from port
  149.  
  150.          test al,1       ; timer bit set?
  151.          jnz s notax
  152.          test _tax,-1    ; value already got?
  153.          jnz s notax
  154.          mov _tax,ecx    ; set axis value
  155.          dec bl          ; port is in, flag one less waiting
  156.          jz found_joyval  ; all ports are in, exit
  157. notax:
  158.          test al,2
  159.          jnz s notay
  160.          test _tay,-1
  161.          jnz s notay
  162.          mov _tay,ecx
  163.          dec bl
  164.          jz found_joyval  ; all ports are in, exit
  165. notay:
  166.          test al,4
  167.          jnz s notbx
  168.          test _tbx,-1
  169.          jnz s notbx
  170.          mov _tbx,ecx
  171.          dec bl
  172.          jz found_joyval  ; all ports are in, exit
  173. notbx:
  174.          test al,8
  175.          jnz s notby
  176.          test _tby,-1
  177.          jnz s notby
  178.          mov _tby,ecx
  179.          dec bl
  180.          jz found_joyval  ; all ports are in, exit
  181. notby:
  182.          inc ecx
  183.          cmp ecx,time_out ; abort if ports fail to arrive in time
  184.          jng joyloop
  185.  
  186. found_joyval:
  187.          sti
  188.          mov eax,_tax     ; -1 = port inactive
  189.          mov ebx,_tay
  190.          mov ecx,_tbx
  191.          mov edx,_tby
  192.  
  193.          ret
  194.  
  195. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  196. ; Justgetbutton:  Get button status
  197. ; In:none
  198. ; Out:
  199. ;   AL - Current button status
  200. ;   DL - Button changed status
  201. ;
  202. ;  7  B  button 2 - 0 = button up,1 = button down
  203. ;  6  B  button 1 - 0 = button up,1 = button down
  204. ;  5  A  button 2 - 0 = button up,1 = button down
  205. ;  4  A  button 1 - 0 = button up,1 = button down
  206. ;  3  B  button 2 - 1 = button up,0 = button down
  207. ;  2  B  button 1 - 1 = button up,0 = button down
  208. ;  1  A  button 2 - 1 = button up,0 = button down
  209. ;  0  A  button 1 - 1 = button up,0 = button down
  210. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  211.  
  212. _justgetbutton:                   ; get present button status
  213.          mov al,_g_thiscallstate  ; save button state for next call
  214.          mov _g_lastcallstate,al
  215.          mov dx, joyport          ; port address of game adapter
  216.          in  al, dx               ; al = game port state
  217.          and al, 0f0h             ; lose lower nibble
  218.          mov dh, al               ; 0f = button down mask
  219.          shr al, 4                ; shift mask to lower nibble
  220.          or  al, dh               ; al = combined button event mask
  221.          xor al, 0f0h             ; f0 = button up mask
  222.          xor ah, ah               ; zero out ah for return
  223.          mov _g_thiscallstate, al ; al = current button status
  224.          mov dx,ax
  225.          xor dl,_g_lastcallstate  ; dl = button changed status
  226.          ret
  227.  
  228. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  229. ; Calibrate_Joystick: Initial joystick grab
  230. ; In=Out=null
  231. ; Notes:
  232. ;  Joystick values are grabbed on entry and calibrated as if  the joystick was
  233. ;  at the center when called.  The joystick re-calculation will not be correct
  234. ;  if the user enters the program with the joystick at non-center, or  if  the
  235. ;  the user turns the turbo off/on while the program is running.
  236. ;
  237. ; The joystick buttons must be in an off state before the routine will continue
  238. ; It does not HAVE to be off, I just do this so that the user will know if  the
  239. ; joystick is stuck up against the monitor or crushed under some books.
  240. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  241.  
  242. _calibrate_joystick:
  243.          call _wait_all_off       ; wait until button released before entering
  244.  
  245.          mov _tax,0               ; clear joystick flags
  246.          mov _tay,0
  247.          mov _tbx,0
  248.          mov _tby,0
  249.          call _rawjoystick        ; get first values
  250.          sub _tax,1               ; 0 becomes -1
  251.          sub _tay,1               ; >0 becomes >=0
  252.          sub _tbx,1
  253.          sub _tby,1
  254.          call _rawjoystick        ; get new values, -1's are inactive ports
  255.  
  256.          cmp _tax,-1              ; test if joystick A valid
  257.          je cal_noa
  258.  
  259.          mov eax,_tax             ; hopefully we have joystick A center
  260.          mov ebx,_tay
  261.          mov _joycenax,eax        ; save center of joystick
  262.          mov _joycenay,ebx
  263. cal_noa:
  264.          cmp _tax,-1              ; test if joystick B valid
  265.          je cal_nob
  266.  
  267.          mov eax,_tbx             ; hopefully we have joystick B center
  268.          mov ebx,_tby
  269.          mov _joycenbx,eax        ; save center of joystick
  270.          mov _joycenby,ebx
  271. cal_nob:
  272.          ret
  273.  
  274. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  275. ; _Cartisian_joystick: Get joystick values in cartisian format.
  276. ; In:
  277. ;   _joycenax - joystick centers, raw data
  278. ;   _joycenay -
  279. ;   _joycenbx -
  280. ;   _joycenby -
  281. ; Out:
  282. ; _joy_ax = EAX = x of joystick a (-16384 to 16384)
  283. ; _joy_ay = EBX = y of joystick a
  284. ; _joy_bx = ECX = x of joystick b
  285. ; _joy_by = EDX = y of joystick b
  286. ;
  287. ; Notes:
  288. ; Assuming calibration routine was entered with  the  joystick  in  the  center
  289. ; position, this routine should be machine independant.
  290. ;
  291. ; The dead_zone is the zone on any axis which the joystick center is registered
  292. ; as  being zero.  This  prevents  the  joystick  from  jittering  when  it  is
  293. ; positioned in the center without anyone touching it.  The  dead_zone  can  be
  294. ; set to a high value when selecting menu items and this will produce a  yes/no
  295. ; response from the joystick.  The zone can then be set back down low  for  the
  296. ; game/demo.
  297. ;
  298. ; The resulting position should be from -16384 to +16384.   But  it  could  be
  299. ; outside this limit if the joystick is not centered correctly  (as  the  real
  300. ; world rarely is centered)
  301. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  302.  
  303. _cartisian_joystick:
  304.          call _rawjoystick        ; get joystick values
  305.  
  306.          mov esi,_dead_zone
  307.          push edx
  308.  
  309.          cmp eax,-1               ; test if joystick A valid
  310.          je car_noa
  311.  
  312.          sub eax,_joycenax        ; convert to cartisian
  313.          sub ebx,_joycenay
  314.  
  315.          shl eax,14               ; get ready for divide
  316.          shl ebx,14
  317.  
  318.          cdq
  319.          mov ebp,_joycenax
  320.          idiv ebp
  321.  
  322.          cmp eax,esi
  323.          jl  car_ca
  324.          sub eax,esi
  325.          jmp car_cc
  326. car_ca:
  327.          neg eax
  328.          cmp eax,esi
  329.          jl  car_cb
  330.          neg eax
  331.          add eax,esi
  332.          jmp car_cc
  333. car_cb:
  334.          xor eax,eax
  335. car_cc:
  336.          mov _joy_ax,eax
  337.  
  338.          mov eax,ebx
  339.          cdq
  340.          mov ebp,_joycenay
  341.          idiv ebp
  342.  
  343.          cmp eax,esi
  344.          jl  car_cz
  345.          sub eax,esi
  346.          jmp car_ce
  347. car_cz:
  348.          neg eax
  349.          cmp eax,esi
  350.          jl  car_cd
  351.          neg eax
  352.          add eax,esi
  353.          jmp car_ce
  354. car_cd:
  355.          xor eax,eax              ; in dead_zone, eax=0
  356. car_ce:
  357.          mov _joy_ay,eax
  358. car_noa:
  359.          pop eax                  ; y co-ordinate of joy B
  360.  
  361.          cmp ecx,-1               ; test if joystick B valid
  362.          je car_nob
  363.  
  364.          sub eax,_joycenbx        ; convert to cartisian
  365.          sub ecx,_joycenby
  366.  
  367.          shl eax,14               ; get ready for divide
  368.          shl ecx,14
  369.  
  370.          cdq
  371.          mov ebp,_joycenbx
  372.          idiv ebp
  373.  
  374.          cmp eax,esi
  375.          jl  car_cf
  376.          sub eax,esi
  377.          jmp car_ch
  378. car_cf:
  379.          neg eax
  380.          cmp eax,esi
  381.          jl  car_cg
  382.          neg eax
  383.          add eax,esi
  384.          jmp car_ch
  385. car_cg:
  386.          xor eax,eax
  387. car_ch:
  388.          mov _joy_bx,eax
  389.  
  390.          mov eax,ebx
  391.          cdq
  392.          mov ebp,_joycenby
  393.          idiv ebp
  394.  
  395.          cmp eax,esi
  396.          jl  car_ci
  397.          sub eax,esi
  398.          jmp car_ck
  399. car_ci:
  400.          neg eax
  401.          cmp eax,esi
  402.          jl  car_cj
  403.          neg eax
  404.          add eax,esi
  405.          jmp car_ck
  406. car_cj:
  407.          xor eax,eax              ; in dead_zone, eax=0
  408. car_ck:
  409.          mov _joy_by,eax
  410. car_nob:
  411.          mov eax,_joy_ax
  412.          mov ebx,_joy_ay
  413.          mov ecx,_joy_bx
  414.          mov edx,_joy_by
  415.  
  416.          ret
  417.  
  418. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  419. ; Wait_all_off: wait for all buttons to return to 0 - abort if ESC pressed
  420. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  421.  
  422. _wait_all_off:
  423.          mov ecx,15               ; wait must be performed 15 times in case buttons are rusty (like mine...)
  424.  
  425. wait_all_off2:
  426.          push ecx
  427.  
  428. wait_all_off3:
  429.          call _justgetbutton
  430.          mov cl,al
  431.          in al,60h                ; esc pressed on keyboard aborts wait.
  432.          cmp al, 1
  433.          je wait_all_off4
  434.          test cl,3*16             ; test any button
  435.          jnz wait_all_off3        ; button up, try again
  436.  
  437. wait_all_off4:
  438.          pop ecx                  ; button down, make sure it is really down
  439.          loop wait_all_off2
  440.  
  441.          ret
  442.  
  443. code32   ends
  444.          end
  445.